import {
  _decorator,
  Animation,
  Collider2D,
  Component,
  Contact2DType,
  director,
  input,
  Input,
  instantiate,
  Label,
  Node,
  Prefab,
  tween,
  Vec3,
} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('player')
export class player extends Component {
  // 获取标靶节点
  @property(Node)
  TargetNode: Node = null;

  // 获取飞刀预制体
  @property(Prefab)
  KnifePrefab: Prefab = null;

  // 获取飞刀父节点
  @property(Node)
  KnifeParentNode: Node = null;

  // 获取提示框节点
  @property(Node)
  TipNode: Node = null;

  // 获取提示框文本
  @property(Label)
  TipLabel: Label = null;

  // 获取底部提示框文本
  @property(Label)
  BottomTipLabel: Label = null;

  // 获取按钮动画
  @property(Animation)
  ButtonAnimation: Animation = null;

  // 初始化旋转角度
  rotateAngle: number = 0;

  // 初始化旋转速度
  rotateSpeed: number = 50;

  // 初始化标靶是否旋转
  targetMove: boolean = true;

  // 目标飞刀数
  targetKnifeNum: number = 10;

  // 当前飞刀数
  nowKnifeNum: number = 0;

  start() {
    this.BottomTipLabel.string =
      '你需要插入' +
      this.targetKnifeNum +
      '把飞刀，目前已经插入' +
      this.nowKnifeNum +
      '把。';
  }

  update(deltaTime: number) {
    if (!this.targetMove) {
      return;
    }
    if (this.rotateAngle >= 360) {
      this.rotateAngle -= 360;
    }
    this.rotateAngle += this.rotateSpeed * deltaTime;
    this.TargetNode.angle = this.rotateAngle;
  }

  protected onLoad(): void {
    input.on(Input.EventType.TOUCH_START, this.touchStart, this);
  }

  protected onDestroy(): void {
    input.off(Input.EventType.TOUCH_START, this.touchStart, this);
  }

  touchStart() {
    // 将飞刀预制体实例化，并赋给飞刀节点
    const knifeNode = instantiate(this.KnifePrefab);
    // 将飞刀节点添加到飞刀父节点下
    knifeNode.setParent(this.KnifeParentNode);
    // 监听飞刀与标靶的碰撞
    knifeNode
      .getComponent(Collider2D)
      .on(Contact2DType.BEGIN_CONTACT, this.beginContact, this);
    // 创建飞刀飞向标靶的缓动动画
    tween(knifeNode)
      .to(0.1, { position: new Vec3(0, 125) })
      .call(() => {
        // 获取飞刀节点的世界坐标
        const worldPosition = knifeNode.getWorldPosition();
        // 将飞刀节点的父节点设置为标靶节点
        knifeNode.setParent(this.TargetNode);
        // 修改飞刀节点的世界坐标
        knifeNode.setWorldPosition(worldPosition);
        // 修改飞刀节点的旋转角度
        knifeNode.angle = -this.TargetNode.angle + 90;
        // 调用updateNowKnifeNum方法
        this.updateNowKnifeNum();
        // 移除飞刀与标靶的碰撞监听
        knifeNode
          .getComponent(Collider2D)
          .off(Contact2DType.BEGIN_CONTACT, this.beginContact, this);
      })
      .start();
  }

  updateNowKnifeNum() {
    if (this.targetMove) {
      this.nowKnifeNum++;
      this.BottomTipLabel.string =
        '你需要插入' +
        this.targetKnifeNum +
        '把飞刀，目前已经插入' +
        this.nowKnifeNum +
        '把。';
    }
    if (this.nowKnifeNum >= this.targetKnifeNum) {
      this.showTip('恭喜你，游戏结束！');
    }
  }

  beginContact() {
    this.showTip('很遗憾，游戏结束！');
  }

  showTip(tip: string) {
    input.off(Input.EventType.TOUCH_START, this.touchStart, this);
    this.targetMove = false;
    this.TipLabel.string = tip;
    this.TipNode.active = true;
    this.ButtonAnimation.play('restart');
  }

  restart() {
    director.loadScene('scene');
  }
}
